package com.wx.mythread.jmm;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 适用于volatile
 */
public class NoVolatile2 implements Runnable{
    volatile boolean done = false;
    AtomicInteger readA = new AtomicInteger();

    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            readA.incrementAndGet();
            flipDone();
        }
    }

    private void flipDone() {
        done = !done;
    }

    public static void main(String[] args) throws InterruptedException {
        NoVolatile2 noVolatile = new NoVolatile2();
        Thread thread1 = new Thread(noVolatile);
        Thread thread2 = new Thread(noVolatile);
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();

        System.out.println(noVolatile.done);
        System.out.println(noVolatile.readA);
    }
}

